package com.rms.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rms.utils.StringUtils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.beanutils.BeanUtils;
import com.rms.entity.FoodType;
import com.rms.service.IFoodTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping(value = "/foodType")
public class FoodTypeAction extends  BaseAction  {



	@RequestMapping(value = "/add")
	private String add(String name, Model model, HttpServletRequest request)
			throws ServletException, IOException {
		try {

			FoodType foodtype = new FoodType();
			foodtype.setTypeName(name);
			foodTypeService.add(foodtype);
			return list(model,request);
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}
	}

	@RequestMapping(value = "/list")
	public String list(Model model,HttpServletRequest request)
			throws ServletException, IOException {
		try {
			List<FoodType> list = foodTypeService.query();
			model.addAttribute("list", list);
			request.getServletContext().setAttribute("foodtype", list);
			return "/sys/foodtype/cuisineList.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}
	}


	
	//在菜系更新中显示菜系对应的类型的名称
	@RequestMapping(value = "/show")
	public String show(int id,Model model)
			throws ServletException, IOException {
		try {
			FoodType type = foodTypeService.findById(id);
			model.addAttribute("type", type);
			return "/sys/foodtype/updateCuisine.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}
	}
	
	@RequestMapping(value = "/update")
	public String update(Model model,HttpServletRequest request){
		try {
			FoodType type = new FoodType();
			Map<String, String[]> map = request.getParameterMap();
			BeanUtils.populate(type, map);
			foodTypeService.updata(type);
			return list(model,request);
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}
	}
	
	@RequestMapping(value = "/delete")
	public String delete(int id,Model model,HttpServletRequest request){
		try {
			foodTypeService.delete(id);
			return list(model,request);
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}
	}


	@RequestMapping(value = "/search")
	public String search(String keyword,Model model,HttpServletRequest request)
	{
		try {

			if(StringUtils.isNull(keyword))
				return list(model,request);

			List<FoodType> list = foodTypeService.query(keyword);
			model.addAttribute("list",list);
			return "/sys/foodtype/cuisineList.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/error/error.jsp";
		}

	}
}
